package com.yanqu.road.dao.impl.activity.gourdchild;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.gourdchild.GourdChildNotice;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GourdChildNoticeDaoImpl extends TempDataObjectDao<GourdChildNotice> {

    public static String getGourdChildNoticeSql = "select * from `t_c_activity_gourd_child_notice` where `activityId`=? order by `time` desc limit ?";

    public List<GourdChildNotice> getGourdChildNotice(int activityId, int limit) {
        List<GourdChildNotice> result = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getGourdChildNoticeSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, limit);
            rs = call.executeQuery();
            while (rs.next()) {
                GourdChildNotice data = parse(rs);
                result.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertGourdChildNoticeSql = "insert into `t_c_activity_gourd_child_notice`(activityId,userId,userBaseInfo,type,rewardId,time) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(GourdChildNotice data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRewardId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTime()));
        if (getDb().executeNoneQuery(insertGourdChildNoticeSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String updateGourdChildNoticeSql = "update `t_c_activity_gourd_child_notice` set userId=?,userBaseInfo=?,type=?,rewardId=?,time=? where activityId=?";
    @Override
    protected boolean updateData(GourdChildNotice data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRewardId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateGourdChildNoticeSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected GourdChildNotice parseData(ResultSet rs) throws SQLException {
        GourdChildNotice data = new GourdChildNotice();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), new TypeReference<UserBaseInfo>() {
        }));
        data.setType(rs.getInt("type"));
        data.setRewardId(rs.getInt("rewardId"));
        data.setTime(rs.getLong("time"));

        return data;
    }
}
